#' 增加表格
#'
#' @param doc  R1_start_docx功能生成的doc
#' @param table 普通table或flextable
#' @param style "table_template"
#' @param add_break 是否在表格后添加分页，默认是
#' @param alignment 列对齐，必须为“l”（左对齐），“r”（右对齐）或“c”（居中）。
#' @param align_table 表格位置，"left", "center" ,"right"
#' @param stylenames officer::table_stylenames()
#' @param first_row 应用或删除表格中第一行的格式
#' @param first_column 应用或删除表格中第一列的格式
#' @param last_row 应用或删除表格中最后一行的格式
#' @param last_column 应用或删除表格中最后一列的格式
#' @param no_hband 不显示奇数行和偶数行
#' @param no_vband 不显示奇数列和偶数列
#'
#' @return 增加表格后的docx
#' @export
#'
#' @examples
#'
#' \dontrun{
#'
#' # 普通table
#' dat <- cbind(data.frame(cars = row.names(mtcars)),mtcars)
#'
#' # flextable,在使用功能前flextable最好先完成格式设置，因为函数很多参数默认对flextable不起作用
#' # 参考书：https://ardata-fr.github.io/flextable-book/index.html
#'
#' # 设置所有新创建的flextable的格式
#' flextable::set_flextable_defaults(font.family = "Times New Roman",
#'  font.size = 11)
#'
#' dat1<- flextable::as_flextable(dat,max_row = 100000,show_coltype = FALSE)
#'
#'
#'
#' doc <- R1_start_docx() %>%
#'   R3_add_table(dat) %>%
#'   R3_add_table(dat1)
#'
#' print(doc, target = "example.docx" )
#'
#' # 删除example.docx
#' unlink("example.docx")
#'
#' }
#'
R3_add_table<-function(doc = doc,
                       table = table,
                       style = "table_template",
                       add_break=TRUE,
                       alignment = NULL, # "l" (left), "r" (right) or "c" (center).
                       align_table = "center",  # "left", "center" or "right"
                       stylenames = officer::table_stylenames(),
                       first_row = TRUE,
                       first_column = FALSE,
                       last_row = FALSE,
                       last_column = FALSE,
                       no_hband = FALSE,
                       no_vband = TRUE){

  require(officer)

  stopifnot(align_table %in% c("left", "center" ,"right") )


  doc<- add_table(table = table,
                  doc = doc,
                  style = style,
                  alignment = alignment,
                  align_table = align_table,
                  stylenames = stylenames,
                  first_row = first_row,
                  first_column = first_column,
                  last_row = last_row,
                  last_column = last_column,
                  no_hband = no_hband,
                  no_vband = no_vband )

  if(add_break){
    doc <- doc %>%
      body_add_break()
  }

  return(doc)
}

add_table<-function(table = table,
                    doc = doc,
                    style = style,
                    alignment = alignment,
                    align_table = align_table,
                    stylenames = stylenames,
                    first_row = first_row,
                    first_column = first_column,
                    last_row = last_row,
                    last_column = last_column,
                    no_hband = no_hband,
                    no_vband = no_vband){
  UseMethod("add_table")
}


add_table.data.frame<-function(table = table,
                               doc = doc,
                               style = style,
                               alignment = alignment,
                               align_table = align_table,
                               stylenames = stylenames,
                               first_row = first_row,
                               first_column = first_column,
                               last_row = last_row,
                               last_column = last_column,
                               no_hband = no_hband,
                               no_vband = no_vband){
  require(officer)
  doc <- doc %>%
    body_add_table(value=table ,
                   style=style,
                   alignment = alignment,
                   align_table = align_table,
                   stylenames = stylenames,
                   first_row = first_row,
                   first_column = first_column,
                   last_row = last_row,
                   last_column = last_column,
                   no_hband = no_hband,
                   no_vband = no_vband)



  return(doc)
}

add_table.flextable<-function(table = table,
                              doc = doc,
                              style = style,
                              alignment = alignment,
                              align_table = align_table,
                              stylenames = stylenames,
                              first_row = first_row,
                              first_column = first_column,
                              last_row = last_row,
                              last_column = last_column,
                              no_hband = no_hband,
                              no_vband = no_vband){
  require(flextable)
  doc <- doc %>%
    body_add_flextable(value=table,align = align_table)
  return(doc)
}

